package cn.ainit.edi.entity;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.jdbc.core.JdbcTemplate;

import cn.ainit.sys.constant.SystemConstant;
import cn.ainit.sys.constant.TypesConstant;
import cn.ainit.sys.utils.DateTimeStr;
import cn.ainit.sys.utils.UUID32;

/***
 * Read data file into DB
 * XML Data   
 * BigBasic Fixed
 * BigBasic Delimiter
 * X12 Fixed
 * X12 Delimiter
 * @version v1.0.0
 * @author xuzq
 *
 */
public class ImportFile {
	private Map<String, List<String>> tableColumnMap;
	private String fileType;
	private String tablePrefix;
	private String filePath;
	
	private Integer flushSize = 15; 
	
	private JdbcTemplate jdbcTemplate;
	//private SessionFactory sessionFactory;
	
	public ImportFile(Map<String, List<String>> tableColumnMap,String filePath,String fileType,String tablePrefix, JdbcTemplate jdbcTemplate) throws Exception {
		this.setTableColumnMap(tableColumnMap);
		this.setFilePath(filePath);//本地路径
		this.setFileType(fileType);
		this.setTablePrefix(SystemConstant.EDI_TABLE_PREFIX+tablePrefix);
		this.setJdbcTemplate(jdbcTemplate);
		/*****Judge this jdbcTemplate is not null***/
		if (this.jdbcTemplate == null) {
			throw new RuntimeException("JdbcTemplate is null, please set jdbcTemplate!");
		}
	}
	
	public boolean startRead() throws Exception{
		try {
			if (fileType.equals(TypesConstant.EDI_XML_DATA)) {
				return readXMLDataFile();
			}else if(fileType.equals(TypesConstant.EDI_BIG_BASIC_FIXED)){
				throw new RuntimeException("没有开通该文件格式导入");
			}else if(fileType.equals(TypesConstant.EDI_BIG_BASIC_DELIMITER)){
				throw new RuntimeException("没有开通该文件格式导入");
			}else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	/***
	 * XML数据   
	 * 1.读取一个节点，获取这个节点指定的节点名
	 * 2.创建sql语句
	 * 3.批量执行sql语句
	 * 4.重复1~3
	 * @return
	 */
	public boolean readXMLDataFile(){
		SAXReader reader = new SAXReader();
		Document document;
		try {
			document = reader.read(new File(this.filePath));
			Element root = document.getRootElement();
			Element koo;
//			//sql = "create table test (a_id varchar2(32) primary key,ainit_id varchar2(32), line number(11), column_bak varchar2(4000));"
			List<String> sqlList = new ArrayList<String>();
			int sqlCount = 0;
			for (Iterator i = root.elementIterator(); i.hasNext();) {
				koo = (Element) i.next();
				sqlCount ++;
				StringBuffer sqls = new StringBuffer(" insert into "+this.tablePrefix);
				StringBuffer sqlValues = new StringBuffer("('");
				sqls.append(koo.getName()+"(");
				List<String> columns = tableColumnMap.get(koo.getName());
				//拼接字段及拼接字段值
				for (int j = 0; j < columns.size(); j++) {
					//建表加入默认字段 a_id varchar2(32) primary key,ainit_id  varchar2(32)
					//line_count number(11), input_date date
					if (j == 0) {
						sqls.append("a_id,ainit_id,");
						sqlValues.append(UUID32.newGuid()+"','"+UUID32.newGuid()+"','");
					}
					sqls.append(columns.get(j));
					sqlValues.append(koo.elementText(columns.get(j))==null?"":koo.elementText(columns.get(j)));
					if (j == columns.size() - 1) {
						sqls.append(",line_count");
						sqlValues.append("','0");
					}
					if (j < columns.size()- 1) {
						sqls.append(",");
						sqlValues.append("','");
					}else {
						sqls.append(") values ");
						sqlValues.append("')");
					}
				}
				sqls.append(sqlValues);
//				System.out.println(koo.getName());
//				if (koo.getName().equals("图书商品")) {
//					System.out.println(koo.elementText("书名"));
//				}
				sqlList.add(sqls.toString());
				//执行sql语句
				if (sqlCount % 50 == 0) {
					for (int j = 0; j < sqlList.size(); j++) {
						System.out.println(sqlList.get(j));
					}
				}
			}
			//执行剩余的SQL语句
			for (int i = 0; i < sqlList.size(); i++) {
				System.out.println(sqlList.get(i));
			}
			//插入到
			System.out.println("共导入"+sqlCount+"条数据");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	/***
	 * BigBasic定长
	 * @return
	 */
	public boolean readBigBasicFixed(){
		return false;
	}
	/***
	 * BigBasic分隔符
	 * @return
	 */
	public boolean readBigBasicDelimiter(){
		return false;
	}
	/***
	 * X12定长
	 * @return
	 */
	public boolean readXFixed(){
		return false;
	}
	/***
	 * X12分隔符
	 * @return
	 */
	public boolean readXDelimiter(){
		return false;
	}
	
	
	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	public String getTablePrefix() {
		return tablePrefix;
	}

	public void setTablePrefix(String tablePrefix) {
		this.tablePrefix = tablePrefix;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public Integer getFlushSize() {
		return flushSize;
	}
	public void setFlushSize(Integer flushSize) {
		this.flushSize = flushSize;
	}
	public Map<String, List<String>> getTableColumnMap() {
		return tableColumnMap;
	}
	public void setTableColumnMap(Map<String, List<String>> tableColumnMap) {
		this.tableColumnMap = tableColumnMap;
	}
	
}
